package com.jingbit.ai.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.util.Log;
import android.util.SparseArray;
import android.view.WindowManager;

import com.jingbit.ai.listener.INetworkListener;

public class BaseApp extends Application {

    public static final String TAG = "jingbit";

    private static SparseArray<Object> baseArray;
    public static BasePreferences preferences;
    public static BaseApp application;

    public static BaseApp getApp() {
        return application;
    }

    private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();

    public WindowManager.LayoutParams getWinParams() {
        return wmParams;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initApplication();
    }

    private void initApplication() {
        Log.d(TAG, "initApp");
        application = this;
        preferences = new BasePreferences();
//        DBManager.initDBManager(this);
//        BaiduManager.initBDManager(this);
//        GlideHelper.initGlideHelper(this);
//        updateManager=new UpdateManager(this);
        baseArray = new SparseArray<Object>();
    }

    public static SparseArray<Object> getBaseArray() {
        return baseArray;
    }

    /**
     * 添加程序监听
     *
     * @param obj
     */
    public static void registerListener(Object obj) {
        Log.d(TAG, "registerListener：" + obj.toString());
        if (baseArray != null) {
            baseArray.put(obj.getClass().hashCode(), obj);
        }
    }

    /**
     * 添加程序监听
     *
     * @param obj
     */
    public static Object getListener(Object obj) {
        Log.d(TAG, "getListener：" + obj.toString());
        if (baseArray != null) {
            return baseArray.get(obj.hashCode());
        }
        return null;
    }

    /**
     * 根据类实列移除程序监空
     *
     * @param obj
     */
    public static void unregisterListener(Object obj) {
        Log.d(TAG, "unregisterActivity：" + obj);
        if (baseArray != null) {
            Object object = baseArray.get(obj.getClass().hashCode());
            baseArray.delete(obj.getClass().hashCode());
            destoryListener(object);
        }
    }

    /**
     * 根据类泛列移除程序监空
     *
     * @param cls
     */
    public static void unregisterListener(Class<?> cls) {
        Log.d(TAG, "unregisterActivity：" + cls);
        if (baseArray != null) {
            Object object = baseArray.get(cls.hashCode());
            baseArray.delete(cls.hashCode());
            destoryListener(object);
        }
    }

    /**
     * 移除有程序监空
     *
     * @param keepService
     */
    public static void unregisterAllListener(boolean keepService) {
        for (int vari = 0; vari < baseArray.size(); vari++) {
            Object object = baseArray.valueAt(vari);
            Log.d(TAG, "unregisterAllActivity：" + object);
            if (keepService && object instanceof Service) {
                continue;
            }
            destoryListener(object);
        }
        baseArray.clear();
    }

    /**
     * 网络状态广播
     *
     * @param networkType
     * @param available
     */
    public static void networkChanged(int networkType, boolean available) {
        for (int vari = 0; vari < baseArray.size(); vari++) {
            Object object = baseArray.valueAt(vari);
            if (object instanceof INetworkListener) {
                ((INetworkListener) object).networkChanged(networkType, available);
            }
        }
    }

    @SuppressLint("NewApi")
    private static void destoryListener(Object object) {
        if (object == null) {
            return;
        }
        if (object instanceof Activity) {
            ((Activity) object).finish();
        } else if (object instanceof Service) {
            ((Service) object).stopSelf();
        } else if (object instanceof Fragment) {
            ((Fragment) object).onDestroy();
        }
    }

    /**
     * 退出app
     *
     * @param logout
     */
    public static void quitApp(boolean logout) {
        if (logout) {
            // 注销后跳转登录界
            unregisterAllListener(false);
        } else {
            unregisterAllListener(false);
            System.exit(0);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.d(TAG, "onLowMemory");
        System.gc();
    }

}